/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2029. All rights reserved.
 * Description: app.c
 * Author:NULL
 * Create: 2019-10-22
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "hi_go.h"
#include "hi_gv.h"
#include "higv_cextfile.h"
#include "hi_gv_parser.h"
#include "hi_gv_adm.h"
#include "hi_gv_ddb.h"
#include "hi_gv_log.h"
#include "higv_language.h"
#include "sample_utils.h"
extern "C"
{
#include "hi_mw_media_intf.h"
}

#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /*  __cplusplus  */

typedef struct {
    HI_U32 widgetHandle;
    HI_U32 keyValue;
    HI_CHAR text[10]; // 10 is text buffer
}HigvKeyMap;

#define PROJECT_ID VIRTUALKB_WIN

#define STD_KEY_NUM 64
#define NUM_KEY_NUM 15
#define EN_KEY_NUM 32
#define CAPEN_KEY_NUM 32

static HigvKeyMap g_keyValueMap[STD_KEY_NUM] = {{softkey_esc, HIGV_KEY_ESC, "Esc"}, {softkey_0, HIGV_KEY_0, "0"},
{softkey_1, HIGV_KEY_1, "1"}, {softkey_2, HIGV_KEY_2, "2"}, {softkey_3, HIGV_KEY_3, "3"}, {softkey_4, HIGV_KEY_4, "4"},
{softkey_5, HIGV_KEY_5, "5"}, {softkey_6, HIGV_KEY_6, "6"}, {softkey_7, HIGV_KEY_7, "7"}, {softkey_8, HIGV_KEY_8, "8"},
{softkey_9, HIGV_KEY_9, "9"}, {softkey_q, HIGV_KEY_q, "q"}, {softkey_w, HIGV_KEY_w, "w"}, {softkey_e, HIGV_KEY_e, "e"},
{softkey_r, HIGV_KEY_r, "r"}, {softkey_t, HIGV_KEY_t, "t"}, {softkey_y, HIGV_KEY_y, "y"}, {softkey_u, HIGV_KEY_u, "u"},
{softkey_i, HIGV_KEY_i, "i"}, {softkey_o, HIGV_KEY_o, "o"}, {softkey_p, HIGV_KEY_p, "p"}, {softkey_a, HIGV_KEY_a, "a"},
{softkey_s, HIGV_KEY_s, "s"}, {softkey_d, HIGV_KEY_d, "d"}, {softkey_f, HIGV_KEY_f, "f"}, {softkey_g, HIGV_KEY_g, "g"},
{softkey_h, HIGV_KEY_h, "h"}, {softkey_j, HIGV_KEY_j, "j"}, {softkey_k, HIGV_KEY_k, "k"}, {softkey_l, HIGV_KEY_l, "l"},
{softkey_dot, HIGV_KEY_DOT, "."}, {softkey_z, HIGV_KEY_z, "z"}, {softkey_x, HIGV_KEY_x, "x"},
{softkey_c, HIGV_KEY_c, "c"}, {softkey_v, HIGV_KEY_v, "v"}, {softkey_b, HIGV_KEY_b, "b"}, {softkey_n, HIGV_KEY_n, "n"},
{softkey_m, HIGV_KEY_m, "m"}, {softkey_del, HIGV_KEY_DEL, "Del"}, {softkey_space, HIGV_KEY_SPACE, "Space"},
{softkey_left, HIGV_KEY_LEFT, "<--"}, {softkey_right, HIGV_KEY_RIGHT, "-->"}, {softkey_sy1, '!', "!"},
{softkey_sy2, '"', "\""}, {softkey_sy3, '#', "#"}, {softkey_sy4, '$', "$"}, {softkey_sy5, '%', "%"},
{softkey_sy6, '&', "&"},{softkey_sy7, '\'', "'"}, {softkey_sy8, '(', "("}, {softkey_sy9, ')', ")"},
{softkey_sy10, '*', "*"}, {softkey_sy11, '+', "+"}, {softkey_sy12, ',', ","}, {softkey_sy13, '-', "-"},
{softkey_sy14, '/', "/"}, {softkey_sy15, ':', ":"}, {softkey_sy16, ';', ";"}, {softkey_sy17, '<', "<"},
{softkey_sy18, '=', "="}, {softkey_sy19, '>', ">"}, {softkey_sy20, '?', "?"}, {softkey_sy21, '@', "@"},
{softkey_sy22, '~', "~"}
};

static HigvKeyMap g_numberValueMap[NUM_KEY_NUM] = {{num_softkey_0, HIGV_KEY_0, "0"}, {num_softkey_1, HIGV_KEY_1, "1"},
{num_softkey_2, HIGV_KEY_2, "2"}, {num_softkey_3, HIGV_KEY_3, "3"}, {num_softkey_4, HIGV_KEY_4, "4"},
{num_softkey_5, HIGV_KEY_5, "5"}, {num_softkey_6, HIGV_KEY_6, "6"}, {num_softkey_7, HIGV_KEY_7, "7"},
{num_softkey_8, HIGV_KEY_8, "8"}, {num_softkey_9, HIGV_KEY_9, "9"}, {num_softkey_esc, HIGV_KEY_ESC, "ESC"},
{num_softkey_del, HIGV_KEY_DEL, "Del"}, {num_softkey_space, HIGV_KEY_SPACE, "SPACE"},
{num_softkey_left, HIGV_KEY_LEFT, "<--"}, {num_softkey_right, HIGV_KEY_RIGHT, "-->"}
};

static HigvKeyMap g_enValueMap[EN_KEY_NUM] = {{en_softkey_a, HIGV_KEY_a, "a"}, {en_softkey_b, HIGV_KEY_b, "b"},
{en_softkey_c, HIGV_KEY_c, "c"}, {en_softkey_d, HIGV_KEY_d, "d"}, {en_softkey_e, HIGV_KEY_e, "e"},
{en_softkey_f, HIGV_KEY_f, "f"}, {en_softkey_g, HIGV_KEY_g, "g"}, {en_softkey_h, HIGV_KEY_h, "h"},
{en_softkey_i, HIGV_KEY_i, "i"}, {en_softkey_j, HIGV_KEY_j, "j"}, {en_softkey_k, HIGV_KEY_k, "k"},
{en_softkey_l, HIGV_KEY_l, "l"}, {en_softkey_m, HIGV_KEY_m, "m"}, {en_softkey_n, HIGV_KEY_n, "n"},
{en_softkey_o, HIGV_KEY_o, "o"}, {en_softkey_p, HIGV_KEY_p, "p"}, {en_softkey_q, HIGV_KEY_q, "q"},
{en_softkey_r, HIGV_KEY_r, "r"}, {en_softkey_s, HIGV_KEY_s, "s"}, {en_softkey_t, HIGV_KEY_t, "t"},
{en_softkey_u, HIGV_KEY_u, "u"}, {en_softkey_v, HIGV_KEY_v, "v"}, {en_softkey_w, HIGV_KEY_w, "w"},
{en_softkey_x, HIGV_KEY_x, "x"}, {en_softkey_y, HIGV_KEY_y, "y"}, {en_softkey_z, HIGV_KEY_z, "z"},
{en_softkey_esc, HIGV_KEY_ESC, "ESC"}, {en_softkey_dot, HIGV_KEY_DOT, "."}, {en_softkey_del, HIGV_KEY_DEL, "Del"},
{en_softkey_space, HIGV_KEY_SPACE, "SPACE"}, {en_softkey_left, HIGV_KEY_LEFT, "<--"},
{en_softkey_right, HIGV_KEY_RIGHT, "-->"}
};

static HigvKeyMap g_capenValueMap[CAPEN_KEY_NUM] = {{capen_softkey_a, HIGV_KEY_A, "A"},
{capen_softkey_b, HIGV_KEY_B, "B"}, {capen_softkey_c, HIGV_KEY_C, "C"}, {capen_softkey_d, HIGV_KEY_D, "D"},
{capen_softkey_e, HIGV_KEY_e, "E"}, {capen_softkey_f, HIGV_KEY_F, "F"}, {capen_softkey_g, HIGV_KEY_G, "G"},
{capen_softkey_h, HIGV_KEY_H, "H"}, {capen_softkey_i, HIGV_KEY_I, "I"}, {capen_softkey_j, HIGV_KEY_J, "J"},
{capen_softkey_k, HIGV_KEY_K, "K"}, {capen_softkey_l, HIGV_KEY_L, "L"}, {capen_softkey_m, HIGV_KEY_M, "M"},
{capen_softkey_n, HIGV_KEY_N, "N"}, {capen_softkey_o, HIGV_KEY_O, "O"}, {capen_softkey_p, HIGV_KEY_P, "P"},
{capen_softkey_q, HIGV_KEY_Q, "Q"}, {capen_softkey_r, HIGV_KEY_R, "R"}, {capen_softkey_s, HIGV_KEY_S, "S"},
{capen_softkey_t, HIGV_KEY_T, "T"}, {capen_softkey_u, HIGV_KEY_U, "U"}, {capen_softkey_v, HIGV_KEY_V, "V"},
{capen_softkey_w, HIGV_KEY_W, "W"}, {capen_softkey_x, HIGV_KEY_X, "X"}, {capen_softkey_y, HIGV_KEY_Y, "Y"},
{capen_softkey_z, HIGV_KEY_Z, "Z"}, {capen_softkey_esc, HIGV_KEY_ESC, "ESC"}, {capen_softkey_dot, HIGV_KEY_DOT, "."},
{capen_softkey_del, HIGV_KEY_DEL, "Del"}, {capen_softkey_space, HIGV_KEY_SPACE, "SPACE"},
{capen_softkey_left, HIGV_KEY_LEFT, "<--"}, {capen_softkey_right, HIGV_KEY_RIGHT, "-->"}
};

static HIGV_HANDLE g_appHandle = 0;

HI_S32 AppCreateSysFont(HI_HANDLE *font)
{
    HI_S32 ret;
    HI_RESID sbFont, mbFont;
    HI_HANDLE fontHandle;
    HIGV_FONT_S fontInfo;
    ret = HI_GV_Res_CreateID(SBFONT_FILE, HIGV_RESTYPE_FONT, &sbFont);
    if (ret != HI_SUCCESS) {
        return ret;
    }

    ret = HI_GV_Res_CreateID(MBFONT_FILE, HIGV_RESTYPE_FONT, &mbFont);
    if (ret != HI_SUCCESS) {
        HI_GV_Res_DestroyID(sbFont);
        return ret;
    }
    fontInfo.MbFontID = mbFont;
    fontInfo.SbFontID = sbFont;
    fontInfo.Size = 0;
    fontInfo.bBold = HI_FALSE;
    fontInfo.bItalic = HI_FALSE;

    ret = HI_GV_Font_Create((const HIGV_FONT_S *)&fontInfo, &fontHandle);
    if (ret != HI_SUCCESS) {
        HI_GV_Res_DestroyID(sbFont);
        HI_GV_Res_DestroyID(mbFont);
        return ret;
    }

    *font = fontHandle;
    return HI_SUCCESS;
}

HIGV_HANDLE g_imeWinHandle = INVALID_HANDLE;

HI_S32 InputMethod_Init()
{
    HigvIMEWindowInit initdata;
    HIGV_STYLE_S winStyle;
    HI_RESID normalSkin = 0;
    HI_S32 ret;
    const HI_S32 candidateNum = 3;
    const HI_S32 sofbKBcnt = 4;

    HI_S32 frameColor = 0xFFFFFF00;
    memset(&winStyle, 0x00, sizeof(winStyle));
    winStyle.StyleType = HIGV_STYLETYPE_COLOR;
    winStyle.BackGround.Color = 0xFF000080;
    winStyle.FontColor = 0xFFFFFFFF;
    winStyle.Top.Color = frameColor;
    winStyle.Bottom.Color = frameColor;
    winStyle.Left.Color = frameColor;
    winStyle.Right.Color = frameColor;
    winStyle.bNoDrawBg = HI_FALSE;
    winStyle.LineWidth = 5; // 5 pixel

    ret = HI_GV_Res_CreateStyle(&winStyle, &normalSkin);

    memset(&initdata, 0x00, sizeof(initdata));
    initdata.pixelFormat = HIGO_PF_BUTT;
    initdata.candidateNum = candidateNum;
    initdata.capEnglishLogoIndex = (HI_U32)"./res/pic/ime/capenglishlogo.PNG";
    initdata.englishLogoIndex = (HI_U32)"./res/pic/ime/englishlogo.PNG";
    initdata.leftArrowPicIndex = (HI_U32)"./res/pic/ime/leftarrowlogo.PNG";
    initdata.numberLogoIndex = (HI_U32)"./res/pic/ime/numberlogo.PNG";
    initdata.pinyinLogoIndex = (HI_U32)"./res/pic/ime/pinyinlogo.PNG";
    initdata.standardALogoIndex = (HI_U32)"./res/pic/ime/englishlogo.PNG";
    initdata.standardBLogoIndex = (HI_U32)"./res/pic/ime/englishlogo.PNG";
    initdata.rightArrowPicIndex = (HI_U32)"./res/pic/ime/rightarrowlogo.PNG";
    initdata.symbolLogoIndex = (HI_U32)"./res/pic/ime/symbollogo.PNG";
    initdata.unActiveLeftArrowPicIndex = (HI_U32)"./res/pic/ime/unactiveleft.PNG";
    initdata.unActiveRightArrowPicIndex = (HI_U32)"./res/pic/ime/unactiveright.PNG";

    ret = HI_GV_IMEWINDOW_Create(HI_NULL, &initdata, &g_imeWinHandle);
    if (ret != HI_SUCCESS) {
        printf("Failed to create the inputmethod window!\n");
        return ret;
    }

    HI_GV_Widget_SetSkin(g_imeWinHandle, HIGV_SKIN_NORMAL, normalSkin);

    /** SoftKey init*/
    HigvIMEWindowSoftKB softKBVec[sofbKBcnt];

    /*Standard soft keyboard,number and english char map*/
    HigvIMEWindowKeyValuePair keyValueMap[STD_KEY_NUM];
    for (HI_S32 j = 0; j < STD_KEY_NUM; j++) {
        keyValueMap[j].keyWidget = g_keyValueMap[j].widgetHandle;
        keyValueMap[j].keyValue = g_keyValueMap[j].keyValue;
        HI_GV_Widget_SetText(keyValueMap[j].keyWidget, g_keyValueMap[j].text);
    }

    /*Number soft keyboard*/
    HigvIMEWindowKeyValuePair numkeyValueMap[NUM_KEY_NUM];
    for (HI_S32 j = 0; j < NUM_KEY_NUM; j++) {
        numkeyValueMap[j].keyWidget = g_numberValueMap[j].widgetHandle;
        numkeyValueMap[j].keyValue = g_numberValueMap[j].keyValue;
        HI_GV_Widget_SetText(numkeyValueMap[j].keyWidget, g_numberValueMap[j].text);
    }

    /*Lowercase soft keyboard*/
    HigvIMEWindowKeyValuePair enkeyValueMap[EN_KEY_NUM];
    for (HI_S32 j = 0; j < EN_KEY_NUM; j++) {
        enkeyValueMap[j].keyWidget = g_enValueMap[j].widgetHandle;
        enkeyValueMap[j].keyValue = g_enValueMap[j].keyValue;
        HI_GV_Widget_SetText(enkeyValueMap[j].keyWidget, g_enValueMap[j].text);
    }

    /*Capital soft keyboard*/
    HigvIMEWindowKeyValuePair capenkeyValueMap[CAPEN_KEY_NUM];
    for (HI_S32 j = 0; j < CAPEN_KEY_NUM; j++) {
        capenkeyValueMap[j].keyWidget = g_capenValueMap[j].widgetHandle;
        capenkeyValueMap[j].keyValue = g_capenValueMap[j].keyValue;
        HI_GV_Widget_SetText(capenkeyValueMap[j].keyWidget, g_capenValueMap[j].text);
    }

    /*Register soft keyboard in the imewindow*/
    memset(softKBVec, 0x0, sizeof(softKBVec));

    softKBVec[HI_GV_IMEWindow_PINYIN].isNoDispIMW = HI_FALSE;
    softKBVec[HI_GV_IMEWindow_PINYIN].softKB = im_softkb;
    softKBVec[HI_GV_IMEWindow_PINYIN].isDispSoftKB = HI_TRUE;
    softKBVec[HI_GV_IMEWindow_PINYIN].keyValueMap = keyValueMap;
    softKBVec[HI_GV_IMEWindow_PINYIN].keyNum = STD_KEY_NUM;

    softKBVec[HI_GV_IMEWindow_CAPENGLISH].isNoDispIMW = HI_TRUE;
    softKBVec[HI_GV_IMEWindow_CAPENGLISH].softKB = im_softkb_capen;
    softKBVec[HI_GV_IMEWindow_CAPENGLISH].isDispSoftKB = HI_TRUE;
    softKBVec[HI_GV_IMEWindow_CAPENGLISH].keyValueMap = capenkeyValueMap;
    softKBVec[HI_GV_IMEWindow_CAPENGLISH].keyNum = CAPEN_KEY_NUM;

    softKBVec[HI_GV_IMEWindow_ENGLISH].isNoDispIMW = HI_TRUE;
    softKBVec[HI_GV_IMEWindow_ENGLISH].softKB = im_softkb_en;
    softKBVec[HI_GV_IMEWindow_ENGLISH].isDispSoftKB = HI_TRUE;
    softKBVec[HI_GV_IMEWindow_ENGLISH].keyValueMap = enkeyValueMap;
    softKBVec[HI_GV_IMEWindow_ENGLISH].keyNum = EN_KEY_NUM;

    softKBVec[HI_GV_IMEWindow_NUMBER].isNoDispIMW = HI_TRUE;
    softKBVec[HI_GV_IMEWindow_NUMBER].softKB = im_softkb_num;
    softKBVec[HI_GV_IMEWindow_NUMBER].isDispSoftKB = HI_TRUE;
    softKBVec[HI_GV_IMEWindow_NUMBER].keyValueMap = numkeyValueMap;
    softKBVec[HI_GV_IMEWindow_NUMBER].keyNum = NUM_KEY_NUM;

    ret = HI_GV_IMEWINDOW_SetSoftKB(g_imeWinHandle, softKBVec, sofbKBcnt);
    if (ret != HI_SUCCESS) {
        printf("HI_GV_IMEWINDOW_SetSoftKB failed! Return: %d\n", ret);
        return ret;
    }

    return HI_SUCCESS;
}

static HI_S32 HI_GV_TP_ReadData(HIGV_TOUCH_INPUTINFO_S* pstInputData)
{
    HI_S32 ret;
    MW_TOUCH_INPUTINFO_S stInputInfo;
    memset(&stInputInfo, 0x0, sizeof(stInputInfo));

    ret = HI_MW_TP_ReadData(&stInputInfo);
    if (ret != HI_SUCCESS) {
        return ret;
    }

    pstInputData->id = stInputInfo.id;
    pstInputData->x  = stInputInfo.x;
    pstInputData->y = stInputInfo.y;
    pstInputData->pressure = stInputInfo.pressure;
    pstInputData->timeStamp = stInputInfo.timeStamp;

    return ret;
}

static HI_S32 HI_GV_Touch_Hook(HIGV_TOUCH_POINT_S* pTouchEvent)
{
    return HI_SUCCESS;
}


HI_S32 HI_GV_TOUCH_Register()
{
    HIGV_TOUCH_DEVICE_INPUT_S stInputDevice = {};
    stInputDevice.OpenDevice = HI_MW_TP_Open;
    stInputDevice.CloseDevie = HI_MW_TP_Close;
    stInputDevice.ReadData   = HI_GV_TP_ReadData;

    (HI_VOID)HI_GV_Gesture_RegisterDevice(&stInputDevice);
    (HI_VOID)HI_GV_Gesture_SetTouchHook(HI_GV_Touch_Hook);

    return HI_SUCCESS;
}

static void* StartHigvApp(void* arg)
{
    HI_S32 ret;
    HI_HANDLE fontHandle;
    HI_HANDLE layerId = INVALID_HANDLE;

    HIGO_LAYER_INFO_S LayerInfo = {SCREEN_WIDTH_VGA, SCREEN_HEIGHT_VGA, SCREEN_HEIGHT_VGA, SCREEN_WIDTH_VGA,
                                   SCREEN_WIDTH_VGA, SCREEN_HEIGHT_VGA,
                                   (HIGO_LAYER_FLUSHTYPE_E)(HIGO_LAYER_BUFFER_DOUBLE), HIGO_LAYER_DEFLICKER_AUTO,
                                   HIGO_PF_8888, HIGO_LAYER_HD_0};

    (HI_VOID)HI_MW_DISP_Open();

    ret = HI_GV_TOUCH_Register();
    if (ret != HI_SUCCESS) {
        printf("HI_GV_TOUCH_Register failed! Return: 0x%x\n", ret);
    }

    ret = HI_GV_Init();
    if (ret != HI_SUCCESS) {
        printf("HI_GV_Init failed! Return: 0x%x\n", ret);
        return NULL;
    }

    ret = HI_GV_PARSER_Init();
    if (ret != HI_SUCCESS) {
        printf("HI_GV_PARSER_Init failed! Return: %d\n", ret);
        return NULL;
    }
    /** Create font res for system default font*/
    ret = AppCreateSysFont(&fontHandle);
    if (ret != HI_SUCCESS) {
        printf("AppCreateSysFont failed! Return: %x\n", ret);
        HI_GV_Deinit();
        HI_GV_PARSER_Deinit();
        return NULL;
    }

    HI_GV_Font_SetSystemDefault(fontHandle);

    /** Register message event of the xml file*/
    HI_GV_PARSER_SetWidgetEventFunc(g_pfunHIGVAppEventFunc, sizeof(g_pfunHIGVAppEventFunc)/sizeof(HI_CHAR *));

    ret = HI_GV_PARSER_LoadFile("./higv.bin");
    if (ret != HI_SUCCESS) {
        printf("HI_GV_PARSER_LoadFile failed! Return: %d\n", ret);
        HI_GV_Deinit();
        HI_GV_PARSER_Deinit();
        return NULL;
    }

    ret = HI_GV_Layer_Create(&LayerInfo, &layerId);
    if (ret != HI_SUCCESS) {
        printf("HI_GV_Layer_CreateEx failed! Return: %x  \n", ret);
        HI_GV_Deinit();
        HI_GV_PARSER_Deinit();
        return NULL;
    }

    ret = HI_GV_Layer_SetRotateMode(layerId, HIGV_ROTATE_90);
    if (ret != HI_SUCCESS) {
        printf("HI_GV_Layer_SetRotateMode failed!: %x\n", ret);
        HI_GV_Layer_Destroy(layerId);
        HI_GV_PARSER_Deinit();
        HI_GV_Deinit();
        return NULL;
    }

    HI_GV_Lan_Register("./res/lan/en.lang",  fontHandle, LAN_EN);
    HI_GV_Lan_Register("./res/lan/cn.lang",  fontHandle, LAN_CN);

    HI_GV_Lan_Change(LAN_CN);

    ret = HI_GV_PARSER_LoadViewById(PROJECT_ID);
    if (ret != HI_SUCCESS) {
        printf("HI_GV_PARSER_LoadViewById failed! Return: %x\n", ret);
        HI_GV_Deinit();
        HI_GV_PARSER_Deinit();
        return NULL;
    }

    ret = InputMethod_Init();
    if (ret != HI_SUCCESS) {
        printf("InputMethod_Init failed! Return: %d\n", ret);
        HI_GV_Deinit();
        HI_GV_PARSER_Deinit();
        return NULL;
    }

    ret = HI_GV_App_Create("softkb", &g_appHandle);
    if (ret != HI_SUCCESS) {
        printf("HI_GV_App_Create failed! Return: %d\n", ret);
        HI_GV_Deinit();
        HI_GV_PARSER_Deinit();
        return NULL;
    }

    ret = HI_GV_Log_SetLevel(HI_NULL, HIGV_LOG_ERROR);
    if (ret != HI_SUCCESS) {
        printf("HI_GV_Log_SetLevel failed! Return: %x\n", ret);
    }

    ret = HI_GV_Widget_Show(PROJECT_ID);
    if (ret != HI_SUCCESS) {
        printf("HI_GV_Widget_Show failed! Return: %d\n", ret);
        HI_GV_Deinit();
        HI_GV_PARSER_Deinit();
        return NULL;
    }

    ret = HI_GV_Widget_Active(PROJECT_ID);
    if (ret != HI_SUCCESS) {
        printf("HI_GV_Widget_Active failed! Return: %d\n", ret);
        HI_GV_Deinit();
        HI_GV_PARSER_Deinit();
        return NULL;
    }

    ret = HI_GV_App_Start(g_appHandle);
    if (ret != HI_SUCCESS) {
        HI_GV_PARSER_Deinit();
        HI_GV_App_Destroy(g_appHandle);
        HI_GV_Deinit();
        return NULL;
    }

    HI_GV_App_Destroy(g_appHandle);
    HI_GV_Layer_Destroy(layerId);
    HI_GV_PARSER_Deinit();
    HI_GV_Deinit();
    HI_MW_DISP_Close();

    return HI_SUCCESS;
}


HI_S32 main(HI_S32 argc, HI_CHAR *argv[])
{
    HI_S32 ret;
    const HI_S32 cmdBufferLen = 10;
    HI_CHAR cmd[cmdBufferLen] = {0};
    pthread_t thread_id = 0;

    ret = pthread_create(&thread_id, NULL, StartHigvApp, NULL);
    if (ret != HI_SUCCESS) {
        printf("pthread_create exec failed \n");
    }

    printf("\nInput CMD: 'quit' or 'q' for quit the program!\n");

    while (fgets(cmd, cmdBufferLen, stdin)) {
        if (strncmp(cmd, "quit", strlen("quit")) == 0) {
            HI_GV_App_Stop(g_appHandle);
            break;
        } else if (strncmp(cmd, "q", 1) == 0) {
            HI_GV_App_Stop(g_appHandle);
            break;
        } else {
            printf("Input CMD: 'quit' or 'q' for quit the program!\n");
        }
    }

    ret = pthread_join(thread_id, NULL);
    if (ret != HI_SUCCESS) {
        printf("pthread_create exec failed \n");
    }
    return HI_SUCCESS;
}


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /*  __cplusplus  */
